package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.SysRoleMapper;
import com.atguigu.spzx.manager.mapper.SysUserRoleMapper;
import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public PageInfo<SysRole> getSysRoleListByPage(Integer page, Integer limit, SysRoleDto sysRoleDto) {
        //开启分页查询
        PageHelper.startPage(page,limit);
        //查询并获取到分页数据
        List<SysRole> sysRoleList =sysRoleMapper.selectSysRoleListByPage(sysRoleDto);
        //把分页数据封装到pageInfo对象里并返回
        PageInfo<SysRole> pageInfo =new PageInfo<>(sysRoleList);
        return pageInfo;
    }

    @Override
    public void updateSysRole(SysRole sysRole) {
        sysRoleMapper.updateSysRole(sysRole);
    }

    @Override
    public void saveSysRole(SysRole sysRole) {
        sysRoleMapper.insertSysRole(sysRole);
    }

    @Override
    public void deleteById(Long roleId) {
        sysRoleMapper.deleteById(roleId);
    }

    @Override
    public Map<String, Object> findAllRoles(Long userId) {
        //查询所有的角色信息
        List<SysRole> sysRoleList= sysRoleMapper.selectAllRoles();
        //查询当前用户的角色id
        List<Long> roleIdsList=sysUserRoleMapper.selectRoleIdsByUserId(userId);
        Map<String, Object> dataMap=new HashMap<>();
        dataMap.put("allRoles",sysRoleList);
        dataMap.put("userRoleIds",roleIdsList);
        return dataMap;
    }


}
